/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */

package cn.knet.sp.ncrm.modules.sys.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * excel导出组件,统一化了部分组件
 *
 * @author zhangbin
 * @version 2015 /05/14 17:37:32
 */
public class ExcelUtils {

	public static final int UNDEFINE = -1;
	public static final int TEXT = 0;
	public static final int INTEGER = 1;
	public static final int FLOAT = 2;

	/**
	 * 表示一列
	 *
	 * @author zhangbin
	 * @date 2014 -11-25 15:11:06
	 * 列中增加总计字段
	 */
	public static class Column {
		private String title; //名称
		private String property; //属性名称,具体使用时为 propertys的队列
		private String format; //格式 date表示为日期;而 html 是将<p>标签替换为\r;seconds-time将秒替换Date;percent是百分比
		private String style; //扩展使用 date时 yyyy-mm-dd的格式,当为seconds-time 用了设置时间显示类型
		private int width = -1; //宽度 此处使用了一个默认不合法的值来表示是否设置

		private boolean total = false;

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getProperty() {
			return property;
		}

		public void setProperty(String property) {
			this.property = property;
		}

		public String getFormat() {
			return format;
		}

		public void setFormat(String format) {
			this.format = format;
		}

		public String getStyle() {
			return style;
		}

		public void setStyle(String style) {
			this.style = style;
		}

		public int getWidth() {
			return width;
		}

		public void setWidth(int width) {
			this.width = width;
		}

		public boolean isTotal() {
			return total;
		}

		public void setTotal(boolean total) {
			this.total = total;
		}

		/**
		 * 将一个属性包含.的进行拆分,拆为一个顺序列
		 * 注意 必须有顺序
		 * @return the propertys
		 */
		public ArrayList<String> getPropertys() {
			ArrayList<String> propertys = new ArrayList<String>();
			if (StringUtils.isNotBlank(property)) {
				String[] keys = property.split("\\.");
				for (String key : keys) {
					propertys.add(key);
				}
			}
			return propertys;
		}


	}

	/**
	 * 定义报表的对象,本质上为一个excel表单
	 * 包含 行 列信息 以及每一列和对象的映射关系
	 * @author zhangbin
	 * @version 2015 /05/14 17:38:27
	 */
	public static class Sheet {

		private List<String> titles = new ArrayList<String>();
		private List<Column> columns = new ArrayList<Column>();
		private String name;
		private List list;
		private boolean isRows;//列出序列

		public Sheet addColumn(String title, String property, String format, String style, int width,boolean total) {
			this.titles.add(title);
			Column column = new Column();
			column.setTitle(title);
			column.setProperty(property);
			column.setFormat(format);
			column.setStyle(style);
			column.setWidth(width);
			column.setTotal(total);
			this.columns.add(column);
			return this;
		}

		public Sheet addColumn(String title, String property, String format, String style, int width) {
			this.titles.add(title);
			Column column = new Column();
			column.setTitle(title);
			column.setProperty(property);
			column.setFormat(format);
			column.setStyle(style);
			column.setWidth(width);
			column.setTotal(false);
			this.columns.add(column);
			return this;
		}

		public Sheet addColumn(String title, String property, String format, String style) {
			addColumn(title,property,format,style,-1);
			return this;
		}

		public Sheet addColumn(String title, String property, String format) {
			addColumn(title, property, format, null);
			return this;
		}

		public Sheet addColumn(String title, String property) {
			addColumn(title, property, null, null);
			return this;
		}

		public List<String> getTitles() {
			return titles;
		}

		public List<Column> getColumns() {
			return columns;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public List getList() {
			return list;
		}

		public void setList(List list) {
			this.list = list;
		}

		public boolean isRows() {
			return isRows;
		}

		public void setRows(boolean isRows) {
			this.isRows = isRows;
		}
	}

	public static int checkValueType(Object value) {
		if (value instanceof Integer) {
			return INTEGER;
		}
		if (value instanceof Double) {
			return FLOAT;
		}
		if (value instanceof Float) {
			return FLOAT;
		}
		if (value instanceof BigDecimal) {
			return FLOAT;
		}
		if (value instanceof BigInteger) {
			return INTEGER;
		}
		if (value instanceof String) {
			return TEXT;
		}
		return UNDEFINE;
	}

	public static String convertText(Object value) {
		return (String) value;
	}

	public static String convertAll(Object value) {
		return value.toString();
	}

	public static double convertFLOAT(Object value) {
		if (value instanceof BigDecimal) {
			BigDecimal bigDecimal = (BigDecimal) value;
			return bigDecimal.floatValue();
		}
		return (Double) value;
	}

	public static int convertInteger(Object value) {
		if (value instanceof BigInteger) {
			BigInteger bigDecimal = (BigInteger) value;
			return bigDecimal.intValue();
		}
		return (Integer) value;
	}
}
